package ar.com.globallogic.promomr.exceptions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import ar.com.globallogic.promomr.api.resources.ErrorResponse;
import ar.com.globallogic.promomr.services.MessageService;

@ControllerAdvice
public class ExceptionsHandler {
	
	private static final String UNEXPECTED_ERROR_CODE = "0000";
	
	private MessageService messageService;
	
	@Autowired	
	public ExceptionsHandler(MessageService messageService) {
		super();
		this.messageService = messageService;
	}

	private ErrorResponse buildResponse(BusinessException exception) {
		String message = messageService.getMessage(exception.getCode(), exception.getParams());
		return new ErrorResponse(exception.getCode(), message);
	}
	
	@ExceptionHandler(DateFormatException.class)
	@ResponseStatus(value = HttpStatus.BAD_REQUEST) 
	public @ResponseBody ErrorResponse handle(DateFormatException exception){
		return buildResponse(exception);
	}

	@ExceptionHandler(UserNameAlreadyExistsException.class)
	@ResponseStatus(value = HttpStatus.BAD_REQUEST) 
	public @ResponseBody ErrorResponse handle(UserNameAlreadyExistsException exception){
		return buildResponse(exception);
	}
	
	@ExceptionHandler(UserNotFoundException.class)
	@ResponseStatus(value = HttpStatus.BAD_REQUEST) 
	public @ResponseBody ErrorResponse handle(UserNotFoundException exception){
		return buildResponse(exception);
	}
	
	@ExceptionHandler(FolderNotFoundException.class)
	@ResponseStatus(value = HttpStatus.BAD_REQUEST) 
	public @ResponseBody ErrorResponse handle(FolderNotFoundException exception){
		return buildResponse(exception);
	}
	
	@ExceptionHandler(DuplicatedNodeNameException.class)
	@ResponseStatus(value = HttpStatus.BAD_REQUEST) 
	public @ResponseBody ErrorResponse handle(DuplicatedNodeNameException exception){
		return buildResponse(exception);
	}
	
	@ExceptionHandler(ContentNotFoundException.class)
	@ResponseStatus(value = HttpStatus.BAD_REQUEST) 
	public @ResponseBody ErrorResponse handle(ContentNotFoundException exception){
		return buildResponse(exception);
	}
	
	@ExceptionHandler(FileUploadException.class)
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) 
	public @ResponseBody ErrorResponse handle(FileUploadException exception){
		return buildResponse(exception);
	}
	
	@ExceptionHandler(PublicationNotFoundException.class)
	@ResponseStatus(value = HttpStatus.BAD_REQUEST) 
	public @ResponseBody ErrorResponse handle(PublicationNotFoundException exception){
		return buildResponse(exception);
	}
	
	@ExceptionHandler(InvalidPublicationStateTransitionException.class)
	@ResponseStatus(value = HttpStatus.BAD_REQUEST) 
	public @ResponseBody ErrorResponse handle(InvalidPublicationStateTransitionException exception){
		return buildResponse(exception);
	}
	
	@ExceptionHandler(ForbiddenContentException.class)
	@ResponseStatus(value = HttpStatus.BAD_REQUEST) 
	public @ResponseBody ErrorResponse handle(ForbiddenContentException exception){
		return buildResponse(exception);
	}
	
	@ExceptionHandler(AnonymousUserException.class)
	@ResponseStatus(value = HttpStatus.UNAUTHORIZED) 
	public @ResponseBody ErrorResponse handle(AnonymousUserException exception){
		return buildResponse(exception);
	}
	
	@ExceptionHandler(RuntimeException.class)
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) 
	public @ResponseBody ErrorResponse handle(RuntimeException exception){
		String message = messageService.getMessage(UNEXPECTED_ERROR_CODE);
		return new ErrorResponse(UNEXPECTED_ERROR_CODE, message);
	}
	
}
